package com.sk.ccflower.server.profile;

import java.sql.Connection;

import org.apache.log4j.Logger;

import com.sk.ccflower.lib.commonUtils.ObjectUtil;
import com.sk.ccflower.lib.define.BaseCfg;
import com.sk.ccflower.lib.define.Consts;
import com.sk.ccflower.lib.pfBeans.ProfileBean;
import com.sk.ccflower.server.lib.dbBeans.CfProfileBean;
import com.sk.ccflower.server.lib.dbUtils.ConnectionPoolManager;
import com.sk.ccflower.server.lib.dbUtils.DBConnection;
import com.sk.ccflower.server.lib.dbUtils.qry.CfProfileQry;
import com.sk.ccflower.server.lib.sysUtils.BaseMain;
import com.sk.ccflower.server.lib.sysUtils.MessageQueueUtil;

public class ThumbnailMaker extends BaseMain {
	
	private Logger m_log = null;
	public ConnectionPoolManager connectionPool = null;
	
	public ThumbnailMaker() throws Exception {
		super();
		
		this.m_log = Logger.getLogger( getClass() );
	}
	
	public static void main( String[] args ) {
		ThumbnailMaker main = null;
		try {
			main = new ThumbnailMaker();
			main.init();
			main.start();
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}
	
	private void init() {
		connectionPool = ConnectionPoolManager.getInstance(BaseCfg.JDBC_URL, BaseCfg.JDBC_USER, BaseCfg.JDBC_PWD);
	}
	
	private void start() {
		System.out.println( "ThumbnailMaker start..." );
		try {
			makeAllThumbmail();
		} catch ( Exception e ) {
			this.m_log.error( e );
			e.printStackTrace();
		}
	}
	
	private void makeAllThumbmail() throws Exception {
		Connection conn = null;
		DBConnection dbConn = null;
		
		try {
			conn = (java.sql.Connection) connectionPool.getConnection();
			dbConn = new DBConnection( conn );
			CfProfileQry pQry = new CfProfileQry( dbConn );
			pQry.select();
			
			for ( int i=0; i<pQry.m_arr.size(); i++ ) {
				CfProfileBean dbBean = (CfProfileBean) pQry.m_arr.get(i);
				ProfileBean bean = new ProfileBean();
				ObjectUtil.objectCopy( bean, dbBean, false );
				
				MessageQueueUtil.sendMsg( BaseCfg.MESSAGE_QUEUE_SERVER_URL, Consts.MESSAGE_QUEUE_NAME.THUMBNAIL, bean );
			}
		} catch ( Exception e ) {
			
		} finally {
			if ( null != conn )		conn.close();
			if ( null != dbConn )	dbConn.close();
		}
	}
	
}
